import 'package:enjoy_plus_bryant/utils/http.dart';
import 'package:enjoy_plus_bryant/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';

class NoticeDetail extends StatefulWidget {
  NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  Map notifyDetail = {};

  Map<String, dynamic> params = {};

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 获取路由参数
    Map agrs = ModalRoute.of(context)!.settings.arguments as Map;
    params = agrs as Map<String, dynamic>;
    getDetail();

    super.didChangeDependencies();
  }

  void getDetail() async {
    try {
      final res = await http.get('/announcement/${params['id']}');
      print(res);

      if (res['code'] != 10000) {
        return ToastUtil.showError('获取公告详情失败');
      }
      ToastUtil.showSuccess('获取公告详情成功');
      setState(() {
        notifyDetail = res['data'];
      });
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('公告详情')),
      body: notifyDetail.isEmpty
          ? Center(child: CircularProgressIndicator())
          : ListView(
              children: [
                Container(
                  padding: const EdgeInsets.all(10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // 标题
                      Text(
                        notifyDetail['title'] ?? '',
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 10),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            notifyDetail['creatorName'] ?? '',
                            style: const TextStyle(color: Colors.grey),
                          ),
                          Text(
                            notifyDetail['createdAt'] ?? '',
                            style: const TextStyle(color: Colors.grey),
                          ),
                        ],
                      ),
                      const SizedBox(height: 10),
                      // 内容 - 解析富文本
                      Html(data: notifyDetail['content'] ?? ''),
                    ],
                  ),
                ),
              ],
            ),
    );
  }
}
